import { Message, MessageBox } from 'element-ui'
const heandels = {
  onmessages: [],
  onopens: []
}
export default{
  onopen: function(handel, event, ws) {
    Message({
      message: '消息服务器连接成功',
      type: 'success',
      duration: 5 * 1000
    })
  },
  onmessage: function(handel, event, ws) {
    heandels.onmessages.forEach(v => {
      v.heandel(JSON.parse(handel.data), ws)
    })
  },
  onclose: function(e, ws) {

  },
  onerror: function(e, ws) {
    Message({
      message: '消息服务器连接失败',
      type: 'error',
      duration: 5 * 1000
    })
  },
  ping: function(ws) {
    ws.send('ping')
  },
  /**
   * 添加新消息监听
   * @param key
   * @param heandel
   */
  addNewMessagHeandel: function(key, heandel) {
    heandels.onmessages.push({ key: key, heandel: heandel })
  }
}
